// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const cow_skin = cc.Class({
    name : "cow_skin",
    properties : {
        cows : {
            default : [],
            type : [cc.SpriteFrame]
        }
    }
});
cc.Class({
    extends: cc.Component,

    properties: {
        cow_sets : {
            default: [],
            type : [cow_skin]
        }
    },




    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //初始化间隔时间
        this.intervalTime = 0;
        this.randomType = Math.floor(Math.random()*3);
    },

    start () {

    },

    update (dt) {
        //间隔时间
        this.intervalTime += dt;
        //每隔0.2秒更换皮肤
        let index = Math.floor(this.intervalTime/0.2);
        index = index%3;
        //获取一种牛的类型
        let cowSet = this.cow_sets[this.randomType];
        //获取精灵组件
        let sprite = this.node.getComponent(cc.Sprite);
        sprite.spriteFrame = cowSet.cows[index];
    },

    runCallBack(){
        cc.log("一个轮回结束");
        this.randomType = Math.floor(Math.random()*3);
    },
});
